package setting_config

import (
	"context"
	"encoding/json"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/logic/withdraw"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/settingtoc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/rpc"
	"github.com/zeromicro/go-zero/core/logc"
)

func OpenRechargeLocalBank(ctx context.Context, setRpc settingtoc.SettingToC) bool {
	res, err := setRpc.DataView(ctx, &settingtoc.DataViewReq{
		MerchantId: withdraw.MerchantIdGet(setRpc, ctx),
		ModelType:  rpc.ModelType_UserAsset,
		Type:       10015, // 是否展示本地充值银行卡列表
	})
	if err != nil {
		logc.Error(ctx, err)
		return false
	}
	var configItem struct {
		Status int `json:"status"`
	}
	if len(res.Data.List) == 1 {
		if err := json.Unmarshal([]byte(res.Data.List[0].Value), &configItem); err != nil {
			logc.Error(ctx, err)
		} else {
			return configItem.Status == 1
		}
	}

	return false
}

func OpenRechargeLocalWallet(ctx context.Context, setRpc settingtoc.SettingToC) bool {
	res, err := setRpc.DataView(ctx, &settingtoc.DataViewReq{
		MerchantId: withdraw.MerchantIdGet(setRpc, ctx),
		ModelType:  rpc.ModelType_Operate,
		Type:       30006, // 是否开启官方充值，下单填入付款人信息弹窗
	})
	if err != nil {
		logc.Error(ctx, err)
		return false
	}
	var configItem struct {
		Status int `json:"status"`
	}
	if len(res.Data.List) == 1 {
		if err := json.Unmarshal([]byte(res.Data.List[0].Value), &configItem); err != nil {
			logc.Error(ctx, err)
		} else {
			return configItem.Status == 1
		}
	}

	return false
}

func GetBankCount(ctx context.Context, setRpc settingtoc.SettingToC) int64 {
	res, err := setRpc.DataView(ctx, &settingtoc.DataViewReq{
		MerchantId: withdraw.MerchantIdGet(setRpc, ctx),
		ModelType:  rpc.ModelType_UserAsset,
		Type:       10027, // 绑定银行卡数
	})
	if err != nil {
		logc.Error(ctx, err)
		return 0
	}
	var configItem struct {
		BankNum int64 `json:"bank_num"`
	}
	if len(res.Data.List) == 1 {
		if err := json.Unmarshal([]byte(res.Data.List[0].Value), &configItem); err != nil {
			logc.Error(ctx, err)
		} else {
			return configItem.BankNum
		}
	}

	return 0
}

func VipConfigCheck(ctx context.Context, setRpc settingtoc.SettingToC) int64 {
	res, err := setRpc.DataView(ctx, &settingtoc.DataViewReq{
		MerchantId: withdraw.MerchantIdGet(setRpc, ctx),
		ModelType:  rpc.ModelType_UserAsset,
		Type:       10012, // 会员等级开关
	})
	if err != nil {
		logc.Error(ctx, err)
		return 0
	}
	var configItem struct {
		BankNum int64 `json:"bank_num"`
	}
	if len(res.Data.List) == 1 {
		if err := json.Unmarshal([]byte(res.Data.List[0].Value), &configItem); err != nil {
			logc.Error(ctx, err)
		} else {
			return configItem.BankNum
		}
	}

	return 0
}
